<?php
#基础函数
include_once('_path.php');
include_once("../includes/init.php");

####变量
#翻页
$page = trim($_GET['page']) + 0;
if ($page < 1) $page = 1;
#搜索
$do = trim($_GET['do']);
#！！搜索
if ($do == 'search') {
	$word = trim($_GET['word']);
}

#总共数量
$sql = "SELECT count(*) total FROM user";
$rs_count = $database->queryArray($sql);
$total_page = ceil($rs_count[0]["total"] / 50);
if ($total_page < 1) $total_page = 1;
if ($page > $total_page) $page = $total_page;

#正式的查询语句
$sql = "SELECT am.* FROM user am WHERE 1 = 1".
iif(strlen($word) > 1, " AND name LIKE '%$word%'").
" ORDER BY id DESC LIMIT ".(($page - 1) * 50).", 50";

#正式查询
$rs = $database->queryArray($sql);

add_log('show', 'user');

$css = array('index', 'table');
$js = array();
include_once("../layout/_header.php");
?>
<div id="path">
	<ul>
		<li><a href="<?php echo _CONST_WEB_URL ?>"><?php echo _CONST_WEB_NAME?></a></li>
		<li><a href="<?php echo _CONST_BACKEND_URL ?>">Backend</a></li>
		<li>User</li>
	</ul>
	<a href="<?php echo _CONST_BACKEND_URL?>logout.php" class="r" style="margin:2px 10px"><span class="logout">Logout</span></a>
</div>

<div id="content">
	<ul id="submenu">
		<li class="current"><a href="./" class="button"><span class="list">List</span></a></li>
		<li ><a href="user_edit.php?func=add" class="button"><span class="add">New</span></a></li>
	</ul>
   <span style="margin:7px 10px 2px 50px;" class="l">Current User:<?php echo $_SESSION['bname']?>&nbsp;&nbsp;<?php echo gmdate('D M j, Y h:ia T', _CONST_TIMENOW) ?></span>

	<div style="float:right;margin-right:10px;">
		<form>
		Name OR Eamil <input type="text" name="word" value="<?php echo htmlspecialchars($word) ?>" size="20" />
		<input type="submit" name="do" value="search" class="button">
		</form>
	</div>
	<?php
	if (sizeof($rs) > 0){
	?>
	<div class="cr"></div>
	<form action="update_user.php" name="form" id="form" method="post">
	<input type="hidden" name="type" value="" />
	<table class="list" cellspacing="0">
		<tr>
			<th class="no">&nbsp;</th>
			<th class="no">ID</a></th>
			<th width="100%" class="no">Name</th>
			<th class="no">Email</th>
			<th class="no">Actived</a></th>
			<th class="no">LastTime</a></th>
			<th class="no">LastIP</a></th>
			<th class="no">Status</a></th>
			<th class="no">&nbsp;</th>
		</tr>
		<?php
		for($i=0;$i<sizeof($rs);$i++) {
			$r = $rs[$i];
			?>
			<tr>
				<td class="no"><input type="checkbox" name="user_ids[]" value="<?php echo $r['id'] ?>"></td>
				<td class="no rt"><?php echo $r['id'] ?></td>
				<td class="no"><?php echo $r['name'] ?></td>
				<td class="no"><?php echo $r['email'] ?></td>
				<td class="no c"><span class="<?php echo iif($r['actived'], "accept", "delete") ?>"></span></td>
				<td class="no"><?php echo gmdate('m/d Y H:i:s', $r['lasttime']) ?></td>
				<td class="no"><?php echo $r['lastip'] ?></td>
				<td class="no c"><span class="<?php echo iif($r['status'], "lock", "unlock") ?>"></span></td>
				<td class="action no">
					<a href="user_edit.php?func=edit&user_id=<?php echo $r['id'] ?>" class="button">
						<img src="<?php echo _CONST_BACKEND_URL ?>media/edit.png" />
						Edit
					</a>
				</td>
			</tr>
			<?php
		} // while ($r = $st->get_row())
		?>
		<tr><td colspan="9" class="action" style="padding:5px;">
			<a href="javascript:checkAll('user_ids[]')" class="button"><span class="on">checkAll</span></a> &nbsp;
			<a href="javascript:clearAll('user_ids[]')" class="button"><span class="off">clearAll</span></a> &nbsp; &nbsp; &nbsp; &nbsp;
			<a href="javascript:document.form.type.value='lock';document.form.submit();" class="button"><span class="lock">Lock</span></a> &nbsp;
			<a href="javascript:document.form.type.value='unlock';document.form.submit();" class="button"><span class="unlock">Unlock</span></a> &nbsp; &nbsp; &nbsp; &nbsp;
		</td></tr>
	</table>
	</form>
	<? if ($total_page > 1) { ?>
	<ul id="nav">
		<li <?= iif($page == 1, 'class="disabled"><a>', '><a href="?page=1">') ?>&lt;&lt;</a></li>
		<li <?= iif($page < 2, 'class="disabled"><a>', '><a href="?page='.($page - 1).'">') ?>&lt;</a></li>
		<li class="jump">
			<form style="vertical-align:middle;">
				<input type="text" name="page" value="<?= $page ?>" size="3" style="float:left;margin-top:1px;width:20px;border:1px #ccc solid;font-size:10px;padding:0 2px;vertical-align:middle;">
				<span style="float:left;padding-left:2px;padding-top:2px;width:50px;vertical-align:middle;"> of <?= $total_page ?></span>
				<input type="image" src="<?= _CONST_BACKEND_URL ?>media/go.png" style="margin-left:4px;float:left;vertical-align:middle;">
			</form>
		</li>
		<li <?= iif($page > $total_page - 1, 'class="disabled"><a>', '><a href="?page='.($page + 1).'">') ?>&gt;</a></li>
		<li <?= iif($page == $total_page, 'class="disabled"><a>', '><a href="?page='.$total_page.'">') ?>&gt;&gt;</a></li>
	</ul>
	<? } ?>
	<?php
	}else{
	?><div class="border cr">No Record</div><?php
	}?>
	</div>
<?php
include_once("../layout/_footer.php");
?>